import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index'

/**
 *设计思路
 *在meta中添加parent 可以将该路由在上方的Sidebar中添加一个父类分组
 * {
 *  path: '/',
 *   component: Layout,
 *   children: [
 *     {
 *       path: 'home',
 *       name: 'Home',
 *       hidden:false,                                      对动态注册的路由，控制该路由是否在Sidebar上显示 默认为false:显示 true:不显示
 *       component: () => import('页面文件路'),              对应的文件
 *       meta:{
 *           title:'Home',                                  页面title 显示名称
 *           parent:'',                                     添加Sidebar上的父分组 无法点击进入  不注册路由  推荐在3级和3级以上使用 在Layout页面添加无效
 *           affix:false,                                   是否标记在tagViews上  默认为false:不固定 true:固定
 *       }
 *     }
 * }
 */
// 公共路由  所有角色都可以访问的路由页面
export const constantRoutes = [
  {
    path: '/redirect',
    hidden: true,
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/fixedPages/redirect/index')
      }
    ]
  },

  {
    path: '/login',
    hidden: true,
    component: () => import('@/fixedPages/login/index'),
    name: 'Login'
  },
  {
    path: '/404',
    hidden: true,
    component: () => import('@/fixedPages/404/index'),
    name: '404'
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index'),
        name: 'Home',
        meta: {
          title: 'Home', icon: 'home', affix: true
        }
      }
    ]
  }

]

// 异步注册的动态路由  需要用户权限  可选由用户动态注册
export const asyncRoutes = [
  {
    path: '/user-center',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/user-center/index'),
        name: 'UserCenter',
        meta: { title: 'UserCenter', icon: 'user-center' }
      }
    ]
  },
  {
    path: '/user-management',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/user-management/index'),
        name: 'UserManagement',
        meta: { title: 'UserManagement', icon: 'user-center' }
      }
    ]
  }

]

export const errorRedirectionRoutes = [
  {
    path: '/(.*)*',
    redirect: '/404',
    hidden: true
  }
]

export const routeMap = {
  Layout: Layout,
  Home: () => import('@/views/home/index'),
  UserCenter: () => import('@/views/user-center/index'),
  UserManagement: () => import('@/views/user-management/index')

}

export const createBaseRouter = (routes = [...constantRoutes, ...asyncRoutes]) => {
  return createRouter({
    // createWebHashHistory    createMemoryHistory  历史模式和hash模式 abstract模式
    history: createWebHashHistory(),
    scrollBehavior: () => ({ top: 0 }),
    routes
  })
}

let router = createBaseRouter()

export function resetRouter(routes) {
  router = createBaseRouter(routes)
}

export default router
